package com.lwh.vo;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description: 通用响应实体
 * @Date: 2019-05-21
 * @author: luowuhui
 */
@Data
public class ResponseVo implements Serializable {
	/**
	 * 响应码
	 */
	private Integer code;
	/**
	 * 响应内容
	 */
	private Object  data;
	/**
	 * 响应信息
	 */
	private String  msg;
	/**
	 * 内容长度
	 */
	private int     count;
	/**
	 * 是否成功响应
	 */
	private boolean isOk;
	/**
	 * 响应内容类型
	 */
	private Class   aClass;

	private ResponseVo (OkBuilder builder) {
		this.code = builder.code;
		this.data = builder.data;
		this.msg = builder.msg;
		this.count = builder.count;
		this.isOk = builder.isOk;
		this.aClass = builder.aClass;
	}

	private ResponseVo (FailBuilder builder) {

		if (builder.code == null) {
			this.code = 400;
		} else {
			this.code = builder.code;
		}
		this.msg = builder.msg;
		this.count = builder.count;
		this.isOk = builder.isOk;
	}

	/**
	 * ok
	 *
	 * @return
	 */
	public static OkBuilder ok () {
		return new OkBuilder();
	}

	/**
	 * 失败
	 *
	 * @return
	 */
	public static FailBuilder fail () {
		return new FailBuilder();
	}

	public static final class OkBuilder {
		private  Class   aClass;
		private Integer code;
		private Object  data;
		private String  msg="success";
		private int     count;
		private boolean isOk = true;

		private OkBuilder () {}

		public OkBuilder code (Integer code) {
			this.code = code;
			return this;
		}

		public OkBuilder data (Object data) {
			this.data = data;
			return this;
		}

		public OkBuilder msg (String msg) {
			this.msg = msg;
			return this;
		}

		public OkBuilder count (int count) {
			this.count = count;
			return this;
		}

		public OkBuilder aClass (Class aClass) {
			this.aClass = aClass;
			return this;
		}

		public ResponseVo build () {
			return new ResponseVo(this);
		}
	}

	public static final class FailBuilder {
		private Integer code;
		private String  msg;
		private int     count = 0;
		private boolean isOk  = false;

		private FailBuilder () {}

		public FailBuilder code (Integer code) {
			this.code = code;
			return this;
		}

		public FailBuilder msg (String msg) {
			this.msg = msg;
			return this;
		}

		public ResponseVo build () {
			return new ResponseVo(this);
		}
	}

	/**
	 * 参数失败
	 *
	 * @return
	 */
	public static ResponseVo badArgument () { return fail(401,"参数不对"); }

	/**
	 * 失败
	 *
	 * @param code
	 * @param msg
	 * @return
	 */
	private static ResponseVo fail (int code,String msg) {
		return fail().code(code)
					 .msg(msg)
					 .build();
	}

	/**
	 * 参数值不对
	 *
	 * @return
	 */
	public static ResponseVo badArgumentValue () { return fail(402,"参数值不对"); }

	/**
	 * 更新数据已经失效
	 *
	 * @return
	 */
	public static ResponseVo updatedDateExpired () { return fail(403,"更新数据已经失效"); }

	/**
	 * 请登录
	 *
	 * @return
	 */
	public static ResponseVo unlogin () { return fail(401,"请登录"); }

	/**
	 * 无页面
	 *
	 * @return
	 */
	public static ResponseVo noPage () { return fail(404,"连接失效"); }

	/**
	 * 认证失效
	 *
	 * @return
	 */
	public static ResponseVo unAuthenticate () { return fail(401,"认证失效"); }

	/**
	 * 系统内部错误
	 *
	 * @return
	 */
	public static ResponseVo serious () { return fail(502,"系统内部错误"); }

	/**
	 * 业务不支持
	 *
	 * @return
	 */
	public static ResponseVo unsupport () { return fail(503,"业务不支持"); }

	/**
	 * 用户未注册
	 *
	 * @return
	 */
	public static ResponseVo unregist () { return fail(504,"用户未注册"); }

	/**
	 * 请求超时
	 *
	 * @return
	 */
	public static ResponseVo outTime () { return fail(408,"请求超时"); }

	/**
	 * 空
	 *
	 * @return
	 */
	public static ResponseVo empty () {
		return ok().code(200)
				   .msg("数据为空")
				   .build();
	}

	public String fromJson () {
		return JSON.toJSONString(this);
	}

	public Class getaClass () {
		if (this.data != null) return this.data.getClass();
		else {
			return null;
		}
	}
}
